var express = require('express');
var router = express.Router();
const uuid = require('uuid');

const team = require("../sql/Team");
const job = require("../sql/Job");
const exchangeRateSQL = require("../sql/ExchangeRate");
const rebatesSQL = require("../sql/Rebates");
const log = require("../sql/Log");

router.get('/', function(req, res, next) {
  const { employeesName } = req.session.userData;

  Promise.all([team.find(), job.find(), exchangeRateSQL.find(), log.find(), rebatesSQL.find()]).then(data=>{
    res.render('setting', {
      employeesName,
      teamData: data[0].reverse(),
      jobData: data[1].reverse(),
      exchangeRateData: data[2].reverse(),
      logData: data[3].reverse(),
      rebatesData: data[4].reverse(),
      index: 6
    });
  }).catch(code=>{
    console.error(code);
    res.send( code );
  })
});

// 添加部门
router.post('/addTeam', function(req, res, next) {
  const {tName} = req.body;
  if(tName){
    team.insertMany({
      tId: 'team-'+uuid.v1(),
      tName,
      tLeaders: []
    }).then(()=>{
      res.redirect('/setting');
    }).catch(code=>{
      res.redirect('/setting');
      console.error(code);
    })
  }else{
    res.redirect('/setting');
  }
});

// 删除部门
router.get('/deleteTeam', function(req, res, next) {
  const {tId} = req.query;
  team.deleteOne({tId}).then(()=>{
    res.redirect('/setting');
  }).catch(code=>{
    res.redirect('/setting');
    console.error(code);
  })
});

// 添加职位
router.post('/addJob', function(req, res, next) {
  const {jName} = req.body;
  if(jName){
    job.insertMany({
      jId: 'job-'+uuid.v1(),
      jName
    }).then(()=>{
      res.redirect('/setting');
    }).catch(code=>{
      res.redirect('/setting');
      console.error(code);
    })
  }else{
    res.redirect('/setting');
  }
});

// 删除职位
router.get('/deleteJob', function(req, res, next) {
  const {jId} = req.query;
  job.deleteOne({jId}).then(()=>{
    res.redirect('/setting');
  }).catch(code=>{
    res.redirect('/setting');
    console.error(code);
  })
});

// 添加汇率
router.post('/addExchangeRate', function(req, res, next) {
  const {exchangeRate, currency} = req.body;
  if(exchangeRate && currency){
    exchangeRateSQL.insertMany({
      eId: 'exchangeRate-'+uuid.v1(),
      currency,
      exchangeRate:exchangeRate-0,
    }).then(()=>{
      res.redirect('/setting');
    }).catch(code=>{
      res.redirect('/setting');
      console.error(code);
    })
  }else{
    res.redirect('/setting');
  }
});

// 修改汇率
router.get('/updateExchangeRate', function(req, res, next) {
  const {eId, exchangeRate} = req.query;

  const updateData = {};
  if( exchangeRate ) updateData.exchangeRate = exchangeRate-0;

  exchangeRateSQL.updateOne({ eId }, updateData).then(()=>{
    res.redirect('/setting');
  }).catch(code=>{
    res.redirect('/setting');
    console.error(code);
  })
});

// 添加提成
router.post('/addRebates', async function(req, res, next) {
  const {rebates} = req.body;
  const tId = req.body.team;

  if(tId && rebates){
    let tName = '';
    try {
      const teamData = await team.find( {tId} );
      tName = teamData[0].tName;
    } catch (error) {
      console.error(error);
      res.send( code );
    }

    rebatesSQL.insertMany({
      eId: 'rebates-'+uuid.v1(),
      team: tName,
      rebates:rebates-0,
    }).then(()=>{
      res.redirect('/setting');
    }).catch(code=>{
      res.redirect('/setting');
      console.error(code);
    })
  }else{
    res.redirect('/setting');
  }
});

// 修改提成
router.get('/updateRebates', function(req, res, next) {
  const {rId, rebates} = req.query;

  const updateData = {};
  if( rebates ) updateData.rebates = rebates-0;

  rebatesSQL.updateOne({ rId }, updateData).then(()=>{
    res.redirect('/setting');
  }).catch(code=>{
    res.redirect('/setting');
    console.error(code);
  })
});

module.exports = router;